我有以下课程:
public class Student {
private Long id ;
private String firstName;
private String lastName;
private Set<Enrollment> enroll = new HashSet<Enrollment>();
//Setters and getters
}
public class Enrollment {
private Student student;
private Course course;
Long enrollId;
//Setters and Getters
}
Run Code Online (Sandbox Code Playgroud)
我有Struts2控制器,我想只返回Class Student的Serialized实例.
@ParentPackage("json-default")
public class JsonAction extends ActionSupport{
private Student student;
@Autowired
DbService dbService;
public String populate(){
return "populate";
}
@Action(value="/getJson", results = {
@Result(name="success", type="json")})
public String test(){
student = dbService.getSudent(new Long(1));
return "success";
}
@JSON(name="student")
public Student …Run Code Online (Sandbox Code Playgroud) 我正在编写我的Servlet应用程序,并希望使用以下静态方法,它将乘以x和y.
public class Helper {
private Helper() {
throw new AssertError();
}
public static int mutltiply(int a, int b) {
int c = a*b;
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Servlets是多线程环境.从servlet调用这样的方法是否安全?
我应该为此功能添加同步属性吗?我的演唱会是关于多线程下c变量的值.
我是Java的新手,所以这些信息会非常有用.
丹尼.
我正在使用ASP.NET中的Spring MVC框架将我的Web应用程序转换为Java(虽然可以学习它的好方法 - :))我需要在我的应用程序中实现身份验证:请告诉我,如果我的方法是好的和专业的,如果不是,最好的做法是什么:
首先,我正在写User class,其中包含有关当前用户firstname/lastname/email/id/etc的所有信息....
class User implements Serializable{
private String firstName;
private String lastName;
private Long id;
private String email;
///Settters and Getters
}
Run Code Online (Sandbox Code Playgroud)
我正在实现名为DlSession的类并在sesison级别上实现它.
<bean id="MySession" class="DlSession" scope="session">
<aop:scoped-proxy/>
class DlSession implements Serializable{
private User currentUser;
public DlSession(){}
// getters and setters:
}
Run Code Online (Sandbox Code Playgroud)
当用户提交他的用户/通行证时,我正在验证凭证,以及用户是否存在将所有用户数据检索到User类的实例.然后我将Session中的currentUser设置为我检索到的用户:
mySesison.setCurrentUser(user);
Run Code Online (Sandbox Code Playgroud)
为了验证身份验证,我需要检查:
if (mySession.getcurrentUser() == null)
//return unauthenticated
else
//return authenticated
Run Code Online (Sandbox Code Playgroud)
要从系统注销用户,我只是这样做:
mySession.setcurrentUser(null);
Run Code Online (Sandbox Code Playgroud)
这种方法是否正确?任何建议都受到欢迎.:)
我正在编写我的SPring MVC Web应用程序.
我将会话时间设置为10080分钟,等于1周.现在我想让用户每次打开浏览器时都会登录:
sessionService.setcurrentUser(myuser);
HttpSession session = request.getSession();
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(timeout);
response.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)
我的cookie Max Age应该与会话超时相同吗?
cookie.setMaxAge(10080);
Run Code Online (Sandbox Code Playgroud)
这是好习惯吗?
我有以下课程:
public class Plugin {
private DistributionManager manager;
public void init(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
manager = context.getBean(DistributionManager.class);
}
public String doSomething(){
String s = manager.doSomething();
return doSomethingElse(s);
}
Run Code Online (Sandbox Code Playgroud)
DistributionManager class本身有很多autowired依赖项并标记为 @Component
现在我想为所有这些代码运行一些单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class PluginTestCase extends AbstractJUnit4SpringContextTests{
@Resource
DistributionManager manager;
@Test
public void testDoSomething(){
Plugin plugin = mock(Plugin.class);
//how can I inject DistributionMamanger bean to plugin using mockito?
assertEquals("MyResult", plugin.doSomething());
}
}
Run Code Online (Sandbox Code Playgroud)
我之前从未使用过mockito.你能帮我模拟插件并完成单元测试吗?
更新:
我根据建议尝试以下测试:
@RunWith(MockitoJUnitRunner.class)
public class PluginTestCase {
@Mock
DistributionManager manager;
@InjectMocks
Plugin testedPlugin; …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Spring MVC 3.1-Release中实现RedirectAttributes功能
我正在发送简单的表单到Post URL,并希望看到我在重定向中发送的值:
我的控制器看起来像这样:
@Controller
public class DefaultController {
@RequestMapping(value="/index.html", method=RequestMethod.GET)
public ModelAndView indexView(){
ModelAndView mv = new ModelAndView("index");
return mv;
}
@RequestMapping(value="/greetings.action", method=RequestMethod.POST)
public ModelAndView startTask(@RequestParam("firstName") String firstName,RedirectAttributes redirectAttributes){
redirectAttributes.addFlashAttribute("redirectAttributes.firstName", firstName);
ModelAndView mv = new ModelAndView(new RedirectView("success.html"));
return mv;
}
@RequestMapping(value="/success.html", method=RequestMethod.GET)
public ModelAndView successView(){
ModelAndView mv = new ModelAndView("success");
return mv;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的Servlet XML看起来像这样:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.vanilla.flashscope.controllers" />
<bean …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建RESTful Web Service,它将根据请求内容类型返回json或xml:
我的控制器看起来像这样:
@Controller
public class RESTController {
@RequestMapping(value="/rest/{id}", method=RequestMethod.GET)
@ResponseBody
public User getUser(@PathVariable Long id){
User user = .....
return user;
}
Run Code Online (Sandbox Code Playgroud)
我的用户类看起来像这样:
@XStreamAlias("user")
public class User {
private long id;
private String firstName;
private String lastName;
other setters and getters..............
}
Run Code Online (Sandbox Code Playgroud)
最后我的Servlet.xml看起来像这样:
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.vanilla.rest.controllers" />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="ignoreAcceptHeader" value="true" />
<property name="favorPathExtension" value="false" />
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property …Run Code Online (Sandbox Code Playgroud) 我正在学习scala,作为最好的培训,我正在将旧的Java算法转换为函数式编程风格.我有以下代码:
def test(originalSet: Set[Int]):Boolean = originalSet match {
case Set() => true
case x::y => false
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于列表,但对于集合,它给出了以下编译错误:
- value Set is not a case class constructor, nor does it have an unapply/unapplySeq
method
Run Code Online (Sandbox Code Playgroud)
和
- constructor cannot be instantiated to expected type; found : scala.collection.immutable.::[B] required:
scala.collection.immutable.Set[Int]
- constructor cannot be instantiated to expected type; found : scala.collection.immutable.::[B] required:
scala.collection.immutable.Set[Int]
Run Code Online (Sandbox Code Playgroud)
问题是什么?如何测试Set为空的情况?如果设置有头尾的话,我怎么能这样呢?
我有以下代码:
trait Calculator {
def add(x:Int, y:Int):Int
def multiply(x:Int,y: Int):Int
}
trait MyCalculator extends Calculator {
override def add(x: Int, y: Int): Int = x+y //in real live it calls remote service which is not avaialble in test
override def multiply(x: Int, y: Int): Int = x*y //in real live it calls remote service which is not avaialble in test
}
object MyCalculator extends MyCalculator
Run Code Online (Sandbox Code Playgroud)
现在我有计算器服务:
trait CalculatorServiceTrait {
def calculate(x:Int,sign:String,y:Int):Int
}
trait CalculatorService extends CalculatorServiceTrait{
override def calculate(x: Int, …Run Code Online (Sandbox Code Playgroud) 我是Scala的新手.
如果我有以下内容List:
val ls = List("a", "a", "a", "b", "b", "c")
Run Code Online (Sandbox Code Playgroud)
如何创建一个Map包含列表中每个元素的多个外观?
例如,Map上面的列表应该是:
Map("a" -> 3, "b" -> 2, "c" -> 1)
Run Code Online (Sandbox Code Playgroud)